home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / progjour / 1991 / 04 / misc.asm < prev    next >
Assembly Source File  |  1991-04-17  |  4KB  |  266 lines

  1.     title    miscellaneous
  2.     include    asm.inc
  3.  
  4.  
  5.     public    close_file
  6.     public    exit_program
  7.     public    get_input_state
  8.     public    open_input_file
  9.     public    open_output_file
  10.     public    move_file_pointer
  11.     public    putchar_newline
  12.     public    putchar_space
  13.     public    put_hex_byte
  14.     public    put_hex_word
  15.     public    read_from_file
  16.     public    save_most
  17.     public    select_min_count
  18.     public    write_to_file
  19.  
  20.  
  21.     .code
  22.     extn    putchar,ms_dos,ms_dos_strerror
  23.  
  24.  
  25. ;;    close file
  26. ;
  27. ;    entry    BX    handle
  28. ;    exit    Cf    0
  29. ;    uses    AX
  30. ;
  31. close_file proc
  32.     mov    ah,3Eh
  33.     jmp    ms_dos_strerror
  34. close_file endp
  35.  
  36.  
  37. ;;    exit program
  38. ;
  39. exit_program proc
  40.     mov    ax,4C00h
  41.     jmp    ms_dos
  42. exit_program endp
  43.  
  44.  
  45. ;;    get input state
  46. ;
  47. ;    exit    Zf    if no key waiting
  48. ;    uses    AX
  49. ;    note    returns TRUE for keyboard, mouse, and timer under Windows
  50. ;
  51. get_input_state proc
  52.     mov    ah,1
  53.     int    16h
  54.     ret
  55. get_input_state endp
  56.  
  57.  
  58. ;;    open input file
  59. ;
  60. ;    entry    DS:SI    string
  61. ;    exit    AX,BX    handle
  62. ;        Cf    if error, error text set
  63. ;    calls    offset_dos_error, ms_dos
  64. ;
  65. open_input_file proc
  66.     mov    ax,3D00h        ; (use 3D20 for shared access read)
  67.     xchg    dx,si
  68.     call    ms_dos_strerror
  69.     xchg    dx,si
  70.     mov    bx,ax
  71.     ret
  72. open_input_file endp
  73.  
  74.  
  75. ;;    open output file
  76. ;
  77. ;    entry    DS:SI    string
  78. ;    exit    AX,BX    handle
  79. ;        Cf    if error, error text set
  80. ;    calls    offset_dos_error, ms_dos
  81. ;
  82. open_output_file proc
  83.     push    cx
  84.     mov    ax,3C00h
  85.     xor    cx,cx
  86.     xchg    dx,si
  87.     call    ms_dos_strerror
  88.     xchg    dx,si
  89.     mov    bx,ax
  90.     pop    cx
  91.     ret
  92. open_output_file endp
  93.  
  94.  
  95. ;;    move file pointer
  96. ;
  97. ;    entry    BX    file handle
  98. ;        DX AX    file position
  99. ;    exit    Cf    if error
  100. ;    uses    AX
  101. ;
  102. move_file_pointer proc
  103.     push    cx
  104.     mov    cx,dx
  105.     mov    dx,ax
  106.     mov    ax,4200h
  107.     call    ms_dos
  108.     pop    cx
  109.     ret
  110. move_file_pointer endp
  111.  
  112.  
  113. ;;    put hex byte
  114. ;
  115. ;    entry    AL    byte
  116. ;    uses    AX
  117. ;    note    calls putchar to write chrs
  118. ;
  119. put_hex_byte proc
  120.     push    ax
  121.     sar    al,1
  122.     sar    al,1
  123.     sar    al,1
  124.     sar    al,1
  125.     call    ohb1
  126.     pop    ax
  127. ohb1:    and    al,0Fh
  128.     add    al,90h
  129.     daa
  130.     adc    al,40h
  131.     daa
  132.     jmp    putchar
  133. put_hex_byte endp
  134.  
  135.  
  136. ;;    put hex word
  137. ;
  138. ;    entry    AX    word
  139. ;    uses    AX
  140. ;
  141. put_hex_word proc
  142.     push    ax
  143.     mov    al,ah
  144.     call    put_hex_byte
  145.     pop    ax
  146.     jmp    put_hex_byte
  147. put_hex_word endp
  148.  
  149.  
  150. ;;    putchar newline
  151. ;
  152. ;    uses    AX
  153. ;
  154. putchar_newline proc
  155.     mov    al,13
  156.     call    putchar
  157.     mov    al,10
  158.     jmp    putchar
  159. putchar_newline endp
  160.  
  161.  
  162. ;;    putchar space
  163. ;
  164. ;    uses    AX
  165. ;
  166. putchar_space proc
  167.     mov    al,' '
  168.     jmp    putchar
  169. putchar_space endp
  170.  
  171.  
  172. ;;    read from file
  173. ;
  174. ;    entry    BX    file handle
  175. ;        CX    byte count
  176. ;        ES:DI    destination
  177. ;    exit    AX    number of bytes read
  178. ;        Cf    if error
  179. ;
  180. read_from_file proc
  181.     push    ds
  182.     mov    ah,3Fh
  183.     push    es
  184.     pop    ds
  185.     xchg    dx,di
  186.     call    ms_dos
  187.     xchg    di,dx
  188.     pop    ds
  189.     ret
  190. read_from_file endp
  191.  
  192.  
  193.     even
  194. ;;    restore most
  195. ;
  196. ;    note    never call this routine
  197. ;
  198. restore_most proc
  199.     popm    bp,es,ds,si,di,dx,cx,bx
  200.     ret
  201. restore_most endp
  202.  
  203.     even
  204. ;;    save most
  205. ;
  206. ;    note    saves all registers except AX and BP.  however, the current
  207. ;        version also saves BP because the code works out that way.
  208. ;        the registers are automatically restored.  this routine is
  209. ;        called with a return address as the top of stack.
  210. ;
  211. save_most proc                ; +16 inner ret adr, +18 outer ret adr
  212.     push    cx            ; +14
  213.     push    dx            ; +12
  214.     push    di            ; +10
  215.     push    si            ; +8
  216.     push    ds            ; +6
  217.     push    es            ; +4
  218.     push    bp            ; +2
  219.     lea    bp,restore_most        ;     after execution of inner
  220.     push    bp            ; +0  routine, return to restore_most
  221.     mov    bp,sp
  222.     xchg    bx,[bp+16]        ;    bx above cx
  223.     push    bx            ; -2    setup return to inner routine
  224.     mov    bx,[bp+16]        ;    restore original BX and BP
  225.     mov    bp,[bp+2]
  226.     ret
  227. save_most endp
  228.  
  229.  
  230. ;;    select min count
  231. ;
  232. ;    entry    CX    count1
  233. ;        DX AX    count2
  234. ;    exit    CX    minimum of count1 and count2
  235. ;
  236. select_min_count proc
  237.     or    dx,dx
  238.     jnz    smc1            ; if count2 > 64k
  239.     cmp    ax,cx
  240.     ja    smc1            ; if count2 > count1
  241.     mov    cx,ax
  242. smc1:    ret
  243. select_min_count endp
  244.  
  245.  
  246. ;;    write to file
  247. ;
  248. ;    entry    BX    file handle
  249. ;        CX    byte count
  250. ;        ES:DI    buffer
  251. ;    exit    Cf    if error
  252. ;
  253. write_to_file proc
  254.     push    ds
  255.     mov    ah,40h
  256.     push    es
  257.     pop    ds
  258.     xchg    dx,di
  259.     call    ms_dos
  260.     xchg    di,dx
  261.     pop    ds
  262.     ret
  263. write_to_file endp
  264.  
  265.     end
  266.